package com.kotlin.tsw.baselibrary.ui.activity

import android.os.Bundle
import com.kotlin.tsw.baselibrary.common.BaseApplication
import com.kotlin.tsw.baselibrary.injection.component.ActivityComponent
import com.kotlin.tsw.baselibrary.injection.component.DaggerActivityComponent
import com.kotlin.tsw.baselibrary.injection.module.ActivityModule
import com.kotlin.tsw.baselibrary.injection.module.LifecycleProviderModule
import com.kotlin.tsw.baselibrary.persenter.BasePresenter
import com.kotlin.tsw.baselibrary.persenter.view.BaseView
import com.kotlin.tsw.baselibrary.widgets.ProgressLoading
import org.jetbrains.anko.toast
import javax.inject.Inject

open abstract class BaseMVPActivity<T : BasePresenter<*>> : BaseActivity(), BaseView {
    override fun showLoading() {
        mDialog.showLoading()
    }

    override fun hideLoading() {
        mDialog.hideLoading()
    }

    override fun onErorr(text:String) {
        toast(text)
    }

    @Inject
    lateinit var mPresenter: T
    lateinit var activityComponent: ActivityComponent
    private lateinit var mDialog: ProgressLoading
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initActivityInjection()
        injectComponent()
        mDialog = ProgressLoading.create(this)
    }

    abstract fun injectComponent()
    private fun initActivityInjection() {
        activityComponent = DaggerActivityComponent.builder()
                .appComponent((application as BaseApplication).appComponent)
                .activityModule(ActivityModule(this))
                .lifecycleProviderModule(LifecycleProviderModule(this))
                .build()
    }
}